#!/bin/bash

if [[ $(id -u) != 0 ]]; then
    echo "Please run as root or use sudo"
    exit 1
fi

set -e

# get ip
IPADDR=$(ip addr | grep -w inet | grep -v "127.0.0.1" | head -1 | awk '{print $2}' | cut -d '/' -f 1)

# check ip
TMP=$(echo ${IPADDR} | grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$")

install_pve()
{
    IP=$1

    # replace ip
    sed -i "s%^127\.0\.1\.1%${IP}%" /etc/hosts

    apt update && apt full-upgrade -y
    apt install -y proxmox-ve open-iscsi

    # do not use enterprise repo
    sed -i 's%^%# %g' /etc/apt/sources.list.d/pve-enterprise.list
}

if [ ${IPADDR} = ${TMP} ]; then
	echo "IP: ${IPADDR}"
    install_pve ${IPADDR}
	echo "web: https://${IPADDR}:8006"
else
	echo "can not found ip address"
	exit
fi

